package com.mszlu.news.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.news.admin.mapper.CommentMapper;
import com.mszlu.news.admin.model.params.CommentParam;
import com.mszlu.news.admin.pojo.Comment;
import com.mszlu.news.admin.service.CommentService;
import com.mszlu.news.admin.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public Result selectComment(CommentParam commentParam) {
        Page<Comment> commentPage=new Page<>(commentParam.getPage(),commentParam.getPageSize());
        IPage<Comment> commentIPage=commentMapper.selectComment(commentPage,commentParam.getId(),commentParam.getContent(),commentParam.getTitle(),commentParam.getAccount());
        return Result.success(commentIPage);
    }

    @Override
    public Result insertUser(CommentParam commentParam) {
        Comment comment=new Comment();
        comment.setContent(commentParam.getContent());
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(commentParam.getAuthorId());
        comment.setCreateDate(commentParam.getCreateDate());
        comment.setParentId(commentParam.getParentId());
        comment.setToUid(commentParam.getToUid());
        if (commentParam.getParentId() == null || commentParam.getParentId() == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        commentMapper.insert(comment);
        return Result.success("添加评论成功");
    }

    @Override
    public Result updateUser(CommentParam commentParam) {
        Comment comment=new Comment();
        comment.setId(commentParam.getId());
        comment.setContent(commentParam.getContent());
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(commentParam.getAuthorId());
        comment.setCreateDate(commentParam.getCreateDate());
        comment.setParentId(commentParam.getParentId());
        comment.setToUid(commentParam.getToUid());
        if (commentParam.getParentId() == null || commentParam.getParentId() == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        commentMapper.updateById(comment);
        return Result.success("更新评论成功");
    }

    @Override
    public Result deleteUserById(Long commentId) {
        commentMapper.deleteById(commentId);
        return Result.success("删除评论成功");
    }
}
